package org.dromara.business.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.GoodsCoupon;
import org.dromara.business.domain.bo.GoodsCouponBo;
import org.dromara.business.domain.vo.GoodsCouponVo;
import org.dromara.business.mapper.GoodsCouponMapper;
import org.dromara.business.service.CouponService;
import org.dromara.business.service.GoodsCouponService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 优惠券Service业务层处理
 *
 * @author zlj
 * @date 2025-06-18
 */
@RequiredArgsConstructor
@Service
public class GoodsCouponServiceImpl extends ServiceImpl<GoodsCouponMapper, GoodsCoupon> implements GoodsCouponService {

    private final GoodsCouponMapper baseMapper;
    private final CouponService couponService;

    /**
     * 查询优惠券
     */
    @Override
    public GoodsCouponVo queryById(Long couponId) {
        return baseMapper.selectVoById(couponId);
    }

    /**
     * 查询优惠券列表
     */
    @Override
    public TableDataInfo<GoodsCouponVo> queryPageList(GoodsCouponBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GoodsCoupon> lqw = buildQueryWrapper(bo);
        Page<GoodsCouponVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询优惠券列表
     */
    @Override
    public List<GoodsCouponVo> queryList(GoodsCouponBo bo) {
        LambdaQueryWrapper<GoodsCoupon> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GoodsCoupon> buildQueryWrapper(GoodsCouponBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GoodsCoupon> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getName()), GoodsCoupon::getName, bo.getName());
        lqw.eq(bo.getAmount() != null, GoodsCoupon::getAmount, bo.getAmount());
        lqw.eq(bo.getConditionAmount() != null, GoodsCoupon::getConditionAmount, bo.getConditionAmount());
        lqw.eq(StrUtil.isNotBlank(bo.getInfo()), GoodsCoupon::getInfo, bo.getInfo());
        lqw.eq(bo.getStartTime() != null, GoodsCoupon::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, GoodsCoupon::getEndTime, bo.getEndTime());
        lqw.orderByDesc(GoodsCoupon::getCreateTime);
        return lqw;
    }

    /**
     * 新增优惠券
     */
    @Override
    public Boolean insertByBo(GoodsCouponBo bo) {
        GoodsCoupon add = MapstructUtils.convert(bo, GoodsCoupon.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCouponId(add.getCouponId());
        }
        return flag;
    }

    /**
     * 修改优惠券
     */
    @Override
    public Boolean updateByBo(GoodsCouponBo bo) {
        GoodsCoupon update = MapstructUtils.convert(bo, GoodsCoupon.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GoodsCoupon entity) {

    }

    /**
     * 批量删除优惠券
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {

        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<GoodsCouponVo> getCoupon() {
        List<GoodsCouponVo> vos = baseMapper.selectVoList(new LambdaQueryWrapper<GoodsCoupon>()
            .le(GoodsCoupon::getStartTime, new Date())
            .ge(GoodsCoupon::getEndTime, new Date()));

        vos.forEach(vo -> vo.setStatus("available"));
//        List<Coupon> haveList = couponService.lambdaQuery().eq(Coupon::getCreateBy, LoginHelper.getUserId()).list();
//        if (CollectionUtil.isNotEmpty(haveList)) {
//            vos.forEach(vo -> {
//                haveList.forEach(coupon -> {
//                    if (vo.getCouponId().equals(coupon.getGoodsCouponId())) {
//                        if (Objects.equals(coupon.getStatus(), "1")) {
//                            vo.setStatus("used");
//                        } else {
//                            vo.setStatus("received");
//                        }
//                    }
//                });
//            });
//        }
        return vos;
    }

}
